Interfacing the CHT8305 Sensor with ESP32: Real-Time Temperature and Humidity Display

The CHT8305 sensor is a reliable temperature and humidity sensor popularly known for its high accuracy and low power consumption. It uses I2C to talk, and the device interfaces easily with most microcontrollers like Arduino and ESP32. Adding a 20×4 I2C LCD display to your setup is a great way to easily monitor real-time temperature and humidity values. In this article, I’ll walk you through how to interface the CHT8305 sensor with both Arduino and ESP32, and we’ll also incorporate the LCD display. I’ll break down each step, provide the code you need, and explain how it works together.

Table of Contents

  1. Overview of the CHT8305 sensor
  2. Components and Pin Connections
  3. Interfacing of CHT8305 with Arduino having 20×4 I2C LCD
  4. Interface of CHT8305 with 20×4 I2C LCD and ESP32
  5. Code Explanation

Overview of the CHT8305 sensor

CHT8305 is a highly integrated sensor solution that measures temperature and relative humidity with high accuracy. Communications are based on the I2C protocol, guaranteeing ease of communication with any microcontroller. This sensor supports voltages from 1.8V to 5.5V; thus, it is compatible with 3.3V logic devices like Arduino and 5V logic devices like ESP32.

Key Features of CHT8305 Temperature and Humidity Sensor

  • Operating Temperature Range: -40°C to +125°C
  • Range of Humidity: 0-100% RH
  • I2C: Fast and efficient data interchanging
  • Accuracy: ± 1°C for temperature, ±2% RH for humidity
  • Low Power Consumption: Suitable for battery-driven applications.

Components and Pin Connections

Required Elements

  • ESP32 Dev Board
  • CHT8305 Temperature and Humidity Sensor
  • 20×4 I2C LCD Display – type LCD1602 or LCD2004
  • Jumper Wires

circuit diagram of esp32 and cht8305 temperature and humidity

Figure 1: Interfacing the CHT8305 Sensor with ESP32

Pin Connections

  • CHT8305 VCC → 3.3V
  • CHT8305 GND → GND
  • CHT8305 SDA → ESP32 GPIO 21
  • CHT8305 SCL → ESP32 GPIO 22
  • LCD VCC → 5V
  • LCD GND → GND
  • LCD SDA → ESP32 GPIO 21
  • LCD SCL → ESP32 GPIO 22

Interfacing of CHT8305 and 20×4 I2C LCD with ESP32

Now, let’s go through the procedure to connect the CHT8305 sensor to ESP32. The code setup for ESP32 is very similar to that of Arduino. Here, you’ll use the same libraries: Wire.h and LiquidCrystal_I2C.h. Now, define SDA and SCL pins for ESP32: GPIO 21 and GPIO 22.

Code Explanation

This code interfaces a CHT8305 temperature and humidity sensor with an ESP32 microcontroller and displays the sensor data on a 20×4 I2C LCD. It reads the temperature and humidity values from the sensor over the I2C protocol and displays them on both the Serial Monitor and the LCD screen. Here’s a detailed breakdown:

Step 1: Initialize I2C Communication and LCD

Examples for both Arduino and ESP32 initialize I2C communication with Wire.begin(). To initialize the I2C LCD display, it informs about its address-commonly 0x27-and the size of the display, which is 20×4.

Reading Sensor Data:

  • readReg(0x00, buf, 4): Reads 4 bytes from the sensor starting at register 0x00. This data includes both temperature and humidity values.
  • Extracting Data:
    • tempData = (buf[0] << 8) | buf[1]: Combines the first two bytes in buf to form the raw temperature data.
    • humData = (buf[2] << 8) | buf[3]: Combines the next two bytes in buf to form the raw humidity data.

Calculating Temperature and Humidity:

  • Temperature: temperature = ((float)tempData * 165 / 65535.0) – 40.0
  • The formula converts the raw temperature data into degrees Celsius, following the CHT8305 sensor’s conversion equation.
  • Humidity: humidity = ((float)humData / 65535.0) * 100
  • The formula converts the raw humidity data into a percentage of relative humidity.

Error Handling:

  • If readReg fails, an error message will be printed on both the serial monitor and the LCD.

readReg Function:

The readReg function reads data from a specific register in the CHT8305 sensor.

  1. Checks for Null Buffer: Ensures that the buffer passed is not NULL.
  2. Starts I2C Transmission:
    • Wire.beginTransmission(CHT8305_I2C_ADDRESS): Initiates communication with the sensor.
    • Wire.write(reg): Sends the register address to the sensor to read data from.
    • Wire.endTransmission(): Ends the transmission and checks if the communication was successful.
  3. Waits for Data Readiness: A 20ms delay to ensure data is ready to be read.
  4. Requests Data: Uses Wire.requestFrom to request a specified number of bytes from the sensor.
  5. Reads Data into Buffer: Stores the data read from the sensor into the provided buffer.
  6. Returns Number of Bytes Read: Returns the size of the data read if successful.

cht8305 i2c temperature and humidity sensor

Conclusion

Interfacing the CHT8305 temperature and humidity sensor with Arduino or ESP32 is quite straightforward and efficient, especially over I2C communication. The sensor’s compactness, moreover, allows it to integrate an I2C LCD display, which will make it ideal for measuring temperature and humidity for several purposes. All you have to do is connect a few components, and your environmental data real-time monitoring will be up and running.

By following this guide, you’ll be able to get your project up and running fast with either Arduino or ESP32 and display temperatures and humidities safely with the CHT8305 sensor. Enjoy building and coding!

Leave a Comment